#include "Metgine/Graphics/Sprite.hpp"

namespace Metgine
{
namespace Graphics
{

Sprite::Sprite()
{
}

Sprite::Sprite(Core::ImageResource* resource, sf::Color hueColor)
{

    m_data.SetImage(resource->Image());

    m_color = hueColor;
    m_data.SetColor(m_color);

}

Sprite::~Sprite()
{

}

const sf::Drawable& Sprite::GetData() const
{
    return m_data;
}


void Sprite::SetPosition(Vector2f& newPosition)
{
    m_data.SetPosition(newPosition.ToSF());
}

void Sprite::SetColor(sf::Color newColor)
{

    m_color = newColor;
    m_data.SetColor(m_color);

}

void Sprite::SetImage(Core::ImageResource* resource)
{

    if (resource)
        m_data.SetImage(resource->Image());

}

void Sprite::SetCenter(float x, float y)
{
    m_data.SetCenter(x, y);
}

void Sprite::Center()
{
    m_data.SetCenter(m_data.GetSize().x / 2, m_data.GetSize().y / 2);
}

void Sprite::SetPosition(float x, float y)
{
    m_data.SetPosition(x, -y);
}

void Sprite::SetRotation(float degrees)
{
    m_data.SetRotation(degrees);
}

void Sprite::SetOrientation(const Vector2f& direction)
{
    float angle = atan2f(direction.y, direction.x);

    if (angle < 0)
        angle += Math::TWO_PI;

       m_data.SetRotation(Math::Degrees(angle));
}

void Sprite::SetScale(float x, float y)
{
    m_data.SetScale(x, y);
}

Vector2f Sprite::GetPosition() const
{

    return Vector2f(m_data.GetPosition());

}

Vector2f Sprite::GetSize() const
{

    return Vector2f(m_data.GetSize());

}

Vector2f Sprite::GetScale() const
{

    return Vector2f(m_data.GetScale());

}

void Sprite::Move(float x, float y)
{

    m_data.Move(x, y);

}

void Sprite::SetAlphaBlend(bool state)
{
    if (state)
        m_data.SetBlendMode(sf::Blend::Alpha);
    else
        m_data.SetBlendMode(sf::Blend::None);
}

float Sprite::GetWidth() const
{
    return m_data.GetSize().x;
}

float Sprite::GetHeight() const
{
    return m_data.GetSize().y;
}

void Sprite::SetMirrored(bool state)
{

    m_data.FlipX(state);

}

void Sprite::SetWidth(float width)
{
    m_data.SetSubRect(sf::IntRect(0, 0, Math::RoundDownToInt(width), Math::RoundDownToInt(m_data.GetSize().y)));
}

}; // Graphics
}; // Metgine
